// pages/editor/person_log_list.js

const app = getApp()
const PAGE_SIZE = 15   //每页大小

Page({

  /**
   * 页面的初始数据
   */
  data: {
    family: 1,   //进入家族
    person: 10000,   //谁的文章
    log_list: [],
    pageNum: 1,   //当前页码
    isLoadEnd: false,   //所有文章是否加载完毕，如果加载完毕就不再加载
    buttons: [{text: '取消'}, {text: '新增'}],
    showDialog: false,
    article_title_focus: false,
    article_title: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.BeginLoadArticles()
  },

  //重新加载整个列表
  BeginLoadArticles() {
    this.setData({
      pageNum: 1,
      isLoadEnd: false,
      log_list: [],
      showDialog: false
    })

    app.httpRequest('get_article_list', {
      family: this.data.family,
      target_person: this.data.person,
      page: this.data.pageNum,
      pagesize: PAGE_SIZE
    }, res => {
      console.log('get_article_list success: ', res)
      if (res.length < PAGE_SIZE) {
        this.setData({
          isLoadEnd: true
        })
      }
      this.data.pageNum++
      this.setData({
        log_list: res
      })
    }, res => {
      console.log('get_article_list failed: ', res)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log("reach bottom ...........")
    this.timer = setInterval(() => {
      this.onBottom()
    }, 2000)
  },

  //触底响应函数
  onBottom(){
    if (this.data.isLoadEnd) {
      return
    }

    app.httpRequest('get_article_list', {
      family: this.data.family,
      target_person: this.data.person,
      page: this.data.pageNum,
      pagesize: PAGE_SIZE
    }, res => {
      console.log('get_article_list success: ', res)
      if (res.length < PAGE_SIZE) {
        this.setData({
          isLoadEnd: true
        })
      }
      this.data.pageNum++
      let lst = this.data.log_list
      this.setData({
        log_list: lst.concat(res)
      })
    }, res => {
      console.log('get_article_list failed: ', res)
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  onClickArticle(e) {
    let articleId = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../editor/person_log_show?article=' +  articleId + '&person=' + this.data.person + '&family=' + this.data.family
    })
  },

  //刷新单一文章的列表数据
  refreshSingleArticle(articleId) {
    app.httpRequest('get_single_article_in_list', {
      family: this.data.family,
      target_person: this.data.person,
      article: articleId
    }, res => {
      console.log('get_single_article_in_list success: ', res)
      //更新相应的文章项目
      let lst = this.data.log_list
      for (let i = 0; i < lst.length; i++) {
        if (lst[i].id == res.id) {
          lst[i] = res
        }
      }
      this.setData({
        log_list: lst
      })

    }, res => {
      console.log('get_single_article_in_list failed: ', res)
    })
  },

  tapAddArticle: function(e) {
    console.log('add article....')
    this.setData({
      showDialog: true,
      article_title_focus: true,
      article_title: ''
    })
  },

  OnArticleTitleInput(e) {
    this.data.article_title = e.detail.value
  },

  tapDialogButton(e) {
    if (e.detail.item.text == "新增") {
      let article_title = this.data.article_title.trim()
      if (article_title.length == 0) {
        app.showTip("文章标题不能为空")
        return
      } else if (article_title.length > 255) {
        app.showTip("文章标题不能超过255个字！")
        return
      }

      app.httpRequest('add_article_by_title', {
        family: this.data.family,
        target_person: this.data.person,
        title: article_title
      }, res => {
        console.log('add_article_by_title success: ', res)
        let articleId = res
        this.setData({
          showDialog: false,
          article_title_focus: false
        })
        wx.navigateTo({
          url: '../editor/edit_person_log?article=' + articleId + '&person=' + this.data.person
        })

      }, res => {
        console.log('add_article_by_title failed: ', res)

        this.setData({
          showDialog: false,
          article_title_focus: false
        })
      })

    } else {
      this.setData({
        showDialog: false,
        article_title_focus: false
      })
    }
  },
})